#include<iostream>
using namespace std;
#include<queue>
const int N=1e5+10;
int l[N],r[N],n;
void PreOrder(int u){
	cout<<u<<" ";
	if(l[u]) PreOrder(l[u]);
	if(l[u]) PreOrder(r[u]);
}
void MidOrder(int u){
	if(l[u]) MidOrder(l[u]);
	cout<<u<<" ";
	if(l[u]) MidOrder(r[u]);
}
void LastOrder(int u){
	if(l[u]) LastOrder(l[u]);
	if(l[u]) LastOrder(r[u]);
	cout<<u<<" ";
}
void bfs(){
	queue<int> q;
	q.push(1);
	while(q.size()){
		int u=q.front();
		q.pop();
		cout<<u<<" ";
		if(l[u]) q.push(l[u]);
		if(r[u]) q.push(r[u]);
	}
} 
int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>l[i]>>r[i];
	}
	PreOrder(1);
	MidOrder(1);
	LastOrder(1);
	return 0;
}
